<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use ZfcUser\View\Helper\ZfcUserIdentity;
use Zend\Session\Container;

class IndexController extends AbstractActionController
{
    protected $em;
    
    public function getEntityManager()
    {
        if (null === $this->em) {
            $this->em = $this->getServiceLocator()
                ->get('doctrine.entitymanager.orm_default');
        }
        return $this->em;
    }

    public function indexAction()
    {
        $sm = $this->getServiceLocator();
        $session = $sm->get('zfcuser_auth_service');
        
        $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        
        /*
    	$manager = \Zend\Session\Container::getDefaultManager();
        if (!$manager->sessionExists()) {
            $manager->start();
        }
        
        $MessageContainer = new Container('message');
        $MessageContainer->messages = $this->getEntityManager()->getRepository('Application\Model\Entity\Message')->findBy(array('destinataire' => $session->getIdentity()->getId()));

        
        $this->layout()->setVariable('messages' , $MessageContainer->messages);
        */
        
        $identity = $session->getIdentity();
        if (!is_null($identity)) {
            $this->layout()->setVariable('messages' , $em->getRepository('Application\Model\Entity\Message')
                ->findBy(array('destinataire' => $identity->getId())));
        }
		
    }
}
